package com.solcoo.merchant.manager;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.android.common.App;
import com.android.common.util.StringUtil;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.model.User;
import com.solcoo.merchant.ui.login.LoginActivity;
import com.solcoo.merchant.ui.menu.MenuActivity;

public class UserManager {

	private static UserManager instance;

	private static User mUser;

	private UserManager() {
	}

	public static UserManager getInstance() {
		if (instance == null) {
			instance = new UserManager();
		}
		return instance;
	}

	public User getUser() {
		if (mUser == null) {
			SharedPreferences localSharedPref = App.CONTEXT
					.getSharedPreferences(Constant.SharedKey.USER,
							Context.MODE_PRIVATE);
			mUser = loadFromSharedPref(localSharedPref);
		}
		return mUser;
	}

	public void setUser(User user) {
		if (user == null) {
			clearUserInfo();
		} else {
			saveToSharedPref(user);
		}
		mUser = user;
	}

	private User loadFromSharedPref(SharedPreferences localSharedPref) {
		User user = new User();
		user.yonghum = localSharedPref.getString("memberNo", StringUtil.EMPTY);
		user.shoujih = localSharedPref.getString("phoneNo", StringUtil.EMPTY);
		user.touxiang = localSharedPref.getString("touxiang", StringUtil.EMPTY);
		user.dianming = localSharedPref.getString("dianming", StringUtil.EMPTY);
		user.lianxifs = localSharedPref.getString("lianxifs", StringUtil.EMPTY);
		user.dizhi = localSharedPref.getString("dizhi", StringUtil.EMPTY);
		if (StringUtil.isBlank(user.yonghum)
				|| StringUtil.isBlank(user.shoujih)) {
			return null;
		}
		return user;
	}

	private void saveToSharedPref(User user) {
		if (user != null) {
			SharedPreferences localSharedPreferences = App.CONTEXT
					.getSharedPreferences(Constant.SharedKey.USER,
							Context.MODE_PRIVATE);
			SharedPreferences.Editor localEditor = localSharedPreferences
					.edit();

			localEditor.putString("memberNo",
					StringUtil.defaultIfBlank(user.yonghum));
			localEditor.putString("phoneNo",
					StringUtil.defaultIfBlank(user.shoujih));
			localEditor.putString("touxiang",
					StringUtil.defaultIfBlank(user.touxiang));
			localEditor.putString("dizhi",
					StringUtil.defaultIfBlank(user.dizhi));
			localEditor.putString("lianxifs",
					StringUtil.defaultIfBlank(user.lianxifs));
			localEditor.putString("dianming",
					StringUtil.defaultIfBlank(user.dianming));
			localEditor.commit();
		}
	}

	private void clearUserInfo() {
		SharedPreferences localSharedPreferences = App.CONTEXT
				.getSharedPreferences(Constant.SharedKey.USER,
						Context.MODE_PRIVATE);
		SharedPreferences.Editor localEditor = localSharedPreferences.edit();
		localEditor.clear();
		localEditor.commit();
	}

	public void loginSuccess(Dialog dialog, Context context, String msg) {
		Intent it = new Intent(context, MenuActivity.class);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(it);
	}

	public void logout() {
		setUser(null);
		Intent it = new Intent(
				ApplicationExt.getInstance().getCurrentContext(),
				LoginActivity.class);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		ApplicationExt.getInstance().getCurrentContext().startActivity(it);
	}

	public boolean isLogin() {
		return UserManager.getInstance().getUser() != null;
	}
}
